package com.sevenstudio.adu.gasing.state;

import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.ui.IGameInterface;

import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.engine.Anchor;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Loading {

	GameEngine engine;
	AScene scene;
	ABackground bg_Loading;
	
	ASprite loading_bar;
	int load;
//	private int COUNT = (int) Utils.GetRatioWidth(iGameDefines.COUNT_SPLASH);
	ASprite[] bar_loading = new ASprite[iGameDefines.COUNT_SPLASH];
	float x = Utils.GetRatioWidth(14);
	float y = Utils.GetRatioHeight(430);
	
	public State_Loading(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
		bg_Loading = new ABackground(engine, iGameDefines.SPRITE_LOADING, 1);


		loading_bar = new ASprite(engine, iGameDefines.SPRITE_LOADING, 4);

		for(int i=0; i < iGameDefines.COUNT_SPLASH; i++)
		{

			bar_loading[i] = new ASprite(engine,iGameDefines.SPRITE_LOADING, 2);
			bar_loading[i].SetPos(x + (i * Utils.GetRatioWidth(20)), y);
			bar_loading[i].theSprite().setVisible(false);
			
		}
	}
	
	public void Init() {
		// TODO Auto-generated method stub
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg_Loading);
	
//		engine.SceneAttach(scene, loading_bar);
		
		for(int i=0; i < iGameDefines.COUNT_SPLASH; i++)
		{
		engine.SceneAttach(scene, bar_loading[i]);
		}
//		engine.SceneAttach(scene, tutup);
//		engine.WaitTimeCapture();
		
//		engine.SetNextMessageState(engine.NextMessageState());
		
	}

	public void Update() {
		
		bar_loading[load / 8].theSprite().setVisible(true);
		load++;
		if(load / 8 == iGameDefines.COUNT_SPLASH)
		{
			engine.SetNextMessageState(engine.NextMessageState());
		}
		
	}

	public void Exit() {
		// TODO Auto-generated method stub
		for(int i =0; i < iGameDefines.COUNT_SPLASH; i++){
			bar_loading[i].DetachSelf();
		}
		bg_Loading.DetachSelf();
		engine.SetNextMessageState(engine.NextMessageState());
		engine.SetNextGameState(engine.NextGameState());
		
	}



}
